﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Globalization;
using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.UI.Modules;

namespace CharityGamesOnline.Modules.Common
{
    public class CGOBase : PortalModuleBase
    {
        protected string GetIconUrl(object dataItem, string type)
        {
            string iconURL = Convert.ToString(DataBinder.Eval(dataItem, type));
            if ((iconURL == string.Empty))
            {
                if ((type == "IconFile"))
                {
                    iconURL = "~/images/icon_unknown_16px.gif";
                }
                else
                {
                    iconURL = "~/images/icon_unknown_32px.gif";
                }
            }

            if (iconURL.IndexOf("FileID=") == 0)
            {
                FileController ctrlFile = new FileController();
                FileInfo objFile = ctrlFile.GetFileById(int.Parse(iconURL.Substring(7)), PortalId);
                iconURL = objFile.RelativePath;
            }

            if (!iconURL.Contains("~") && !iconURL.Contains("http"))
            {
                iconURL = System.IO.Path.Combine(PortalSettings.HomeDirectory, iconURL);
            }

            return ResolveUrl(iconURL);
        }

        protected DateTime ConvertDateStringstoDatetime(string strDate, string strTime)
        {
            CultureInfo invCulture = CultureInfo.InvariantCulture;
            DateTime tDate = new DateTime();
            tDate = Convert.ToDateTime(strDate).Date;
            //Since dates may not be in a form directly combinable with time, convert back to string to enable combination
            strDate = tDate.ToString("yyyy/MM/dd", invCulture);
            tDate = DateTime.ParseExact(strDate + " " + strTime, "yyyy/MM/dd HH:mm", invCulture);
            return tDate;
        }

        protected T FindControl<T>(Control ctlParent)
        {
            T tRet = default(T);
            foreach (Control objCtl in ctlParent.Controls)
            {
                if (objCtl is T)
                {
                    IModuleControl iModule = (IModuleControl)objCtl;
                    tRet = (T)iModule;
                    //tRet = (T)Convert.ChangeType(objCtl, typeof(T));
                    break;
                }
            }
            return tRet;
        }

        public string FormatString(string formatStr, int maxLength, string srcStr)
        {
            if (srcStr.Length > maxLength)
            {
                return string.Format(formatStr, srcStr.Substring(0, maxLength));
            }
            else
                return srcStr;
        }
    }
}
